//
//  CheLiangDetailViewController.m
//  ZuLinProject
//
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXCustomerDeatilViewController.h"
#import "LXCustomerDetailsTopView.h"
#import "CheLiangOperateListCell.h"
#import "LXCustomerApplyViewController.h"
#import "KeHuInfoModel.h"

#import "LXInsuranceUseRecordViewController.h"
#import "LXCarSignListViewController.h"
#import "LXInsuranceRecordViewController.h"
#import "LXYearCheckRecordViewController.h"
#import "LXMaintainRecordViewController.h"
#import "LXRepairRecordViewController.h"
#import "LXDepositRecordViewController.h"
#import "LXDelayRecordViewController.h"
#import "LXPaymentRecordViewController.h"

#define kItemHeight KHeightScale(95)
#define kItemCount 3
@interface LXCustomerDeatilViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (strong, nonatomic) UICollectionView *collectionView;

@property (nonatomic, strong) NSArray *data;
@property (nonatomic, strong) LXCustomerDetailsTopView *topView;
@property (nonatomic, strong) KeHuInfoModel *model;

@end

@implementation LXCustomerDeatilViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (kUserDefaultForBool(kRefreshStr)) {
        kUserDefaultSetBool(NO, kRefreshStr);
        [self loadData];
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#FAFBFC"];
    [self layoutCustomNav:self.view title:@"客户租车详情"];

    
    LXCustomerDetailsTopView *topView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([LXCustomerDetailsTopView class]) owner:nil options:nil].firstObject;
    topView.frame = CGRectMake(0, kViewTopHeight, ScreenWidth, 115);
    self.topView = topView;
    [self.view addSubview:topView];
    [topView.btn5 whenTapped:^{
        LXCustomerApplyViewController *vc = [[LXCustomerApplyViewController alloc] init];
        vc.type = 2;
        vc.kehuModel = self.model;
        [self pushToNextController:vc];
    }];
    
    [topView.btn6 whenTapped:^{
        [MSUtils callPhone:self.model.customerPhone];
    }];
    
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake((kScreenWidth - 42)/kItemCount,kItemHeight);
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 10, 10, 10);
    flowLayout.minimumLineSpacing= 10;
    flowLayout.minimumInteritemSpacing = 10;
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, topView.Y_height + 10, ScreenWidth, kScreenHeight - topView.Y_height) collectionViewLayout:flowLayout];
    [self.view addSubview:collectionView];
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = ColorWithF5F5F5;
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CheLiangOperateListCell  class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([CheLiangOperateListCell class])];
    self.collectionView = collectionView;
    

    
    self.data = @[
        @{
            @"title":@"签约记录",
            @"img":@"签约记录_return",
        },
        @{
            @"title":@"收/付款记录",
            @"img":@"收付款记录_return",
        },
        @{
            @"title":@"退押金记录",
            @"img":@"退押金记录_return",
        },
        @{
            @"title":@"维修记录",
            @"img":@"维修记录_return",
        },
        @{
            @"title":@"延期记录",
            @"img":@"延期记录_return",
        },
        
        @{
            @"title":@"保养记录",
            @"img":@"保养记录_return",
        },
        @{
            @"title":@"出险记录",
            @"img":@"出险记录_return",
        }
       
    
    
    ];
    [self loadData];

}
-(void)loadData{
    [MSNetworkManager getRevertUrl:GET_TCustomerDeatil params:@{@"id":self.ID} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        if (isSuccess) {
            KeHuInfoModel *model = [KeHuInfoModel mj_objectWithKeyValues:resonseObj];
            
            
            if (!StringIsEmpty(self.beginTime) || !StringIsEmpty(self.endTime)) {
                model.startTime = self.beginTime;
                model.endTime = self.endTime;
            }
            
            
            self.topView.model = model;
            self.model = model;
            
            
            
            
        }
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
}
#pragma mark ---- UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.data.count;
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CheLiangOperateListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[CheLiangOperateListCell cellID] forIndexPath:indexPath];
    NSDictionary *dic = self.data[indexPath.row];
    cell.imageV.image = [UIImage imageNamed:dic[@"img"]];
    cell.titleL.text = dic[@"title"];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    

    switch (indexPath.row) {
        case 0://  签约记录
        {
            LXCarSignListViewController *vc = [[LXCarSignListViewController alloc] init];
            vc.customerId = self.model.ID;
            [self pushToNextController:vc];
            
            break;
        }
        case 1: // 收付款记录
        {
            LXPaymentRecordViewController *vc = [[LXPaymentRecordViewController alloc] init];
            vc.customerId = self.model.ID;
            vc.type = 1;
            [self pushToNextController:vc];
            break;
        }
        case 2:   // 退押金记录
        {
            LXDepositRecordViewController *vc = [[LXDepositRecordViewController alloc] init];
            vc.type = 1;
            vc.customerId = self.model.ID;
            [self.navigationController pushViewController:vc animated:YES];
            
        }
            break;
        case 3://维修记录
        {
            LXRepairRecordViewController *vc = [[LXRepairRecordViewController alloc] init];
            vc.type = 1;
            vc.customerId = self.model.ID;
            [self pushToNextController:vc];
            break;
        }
        case 4://延期记录
        {
            LXDelayRecordViewController *vc = [[LXDelayRecordViewController alloc] init];
            vc.type = 1;
            vc.customerId = self.model.ID;
            [self.navigationController pushViewController:vc animated:YES];
            break;
            
        }
        case 5://保养记录
        {
            LXMaintainRecordViewController *vc = [[LXMaintainRecordViewController alloc] init];
            vc.type = 1;
            vc.customerId = self.model.ID;
            [self pushToNextController:vc];
            break;
        }
        case 6://出险记录
        {
            LXInsuranceUseRecordViewController *vc = [[LXInsuranceUseRecordViewController alloc] init];
            vc.type = 1;
            vc.customerId = self.model.ID;
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        
        
        default:
            showAlert(@"此功能暂未开放");
            break;
    }
}



@end
